﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace lianxi
{
    public partial class index : System.Web.UI.Page
    {
        public void chaxun() {
            string sql1 = string.Format("select * from [user]");
            DataSet ds = DBHelper.GetDataSet(sql1);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                //string sql = string.Format("select * from [user]");
                //DataSet ds = DBHelper.GetDataSet(sql);
                //this.GridView1.DataSource = ds;
                //this.GridView1.DataBind();
                chaxun();
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
            string sql = string.Format("delete from [user] where id="+id);
            int result = DBHelper.ExecuteNonQuery(sql);
            if (result>0)
            {
                Response.Write("<script>alert('删除成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }
            chaxun();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            chaxun();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
            string name = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            string add = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            string sql = string.Format("update [user] set name='{0}',aary='{1}' where id={2}",name,add,id);
            int resuls = DBHelper.ExecuteNonQuery(sql);
            if (resuls>0)
            {
                Response.Write("<script>alert('修改成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('修改失败')</script>");
            }
            this.GridView1.EditIndex = -1;
            chaxun();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            chaxun();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = this.TextBox1.Text.ToString();
            string add = this.TextBox2.Text.ToString();
            string sql = string.Format("insert into [user](name,aary) values('{0}','{1}')",name,add);
            int resuts = DBHelper.ExecuteNonQuery(sql);
            if (resuts>0)
            {
                Response.Write("<script>alert('添加成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('添加失败')</script>");
            }
            chaxun();
        }
    }
}